#include "stdafx.h"
#include "Control.h"
Control::Control()
{
	this->flock = new Flock;
	analysis = new BehaviorAnalysis;
	maxSteps = 0;
	nStep = 0;
	renderingEnabled = false;
}


Control::Control( Configuration& config )
{
	if( config.configurationEnabled == false)
	{
		cout<<"Control: configuration not enabled."<<endl;
	}
	else
	{
		flock = new Flock( config );
		analysis = new BehaviorAnalysis( config );
		this->maxSteps = config.maxSteps;
		this->nStep = 0;
		this->renderingEnabled = config.renderingEnabled;
	}
}

bool Control::initialize()
{
	if ( !this->flock->init() )
	{
		cout<<"Control: error while initializing flock."<<endl;
		return false;
	}
	if ( !analysis->init(flock) )
	{
		cout<<"Control : error while initializing analysis."<<endl;
		return false;
	}
	if ( this->analysis->fileheadEnabled == true )
		this->analysis->writeFileHead( this->configuration );
  return true;
}

void Control::setConfiguration( Configuration& config)
{
	if( config.configurationEnabled == false )
	{
		cout<<"Control: configuration not enabled."<<endl;
	}
	else
	{
		this->configuration = config;
		flock->setConfiguration( config );
		analysis->setConfiguration( config );
		this->maxSteps = config.maxSteps;
		this->nStep = 0;
		this->renderingEnabled = config.renderingEnabled;
	}
}

Control::~Control()
{
	delete flock;
	delete analysis;
}

void Control::simulate( )
{
	if ( this->renderingEnabled == false )
	{
		while ( nStep < maxSteps )
		{
			this->flock->move();
			this->analysis->analyze( this->flock );
			nStep++;
		}
	}
	else
	{
		this->flock->move();
		if (nStep % 10 ==0) this->analysis->analyze(  this->flock);
		nStep++;
		if ( nStep > maxSteps) exit(0);
	}
}

